package cn.com.czpb.client.module.news.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import cn.com.czpb.client.R;
import cn.com.czpb.client.app.MToolBarActivity;
import cn.com.czpb.client.app.MyActivitymanager;
import cn.com.czpb.client.custorm.smartrefreshalayout.ClassHeader_other;
import cn.com.czpb.client.model.news.NewsBean;
import cn.com.czpb.client.model.news.NewsListEntity;
import cn.com.czpb.client.model.news.SubJectTypeBean;
import cn.com.czpb.client.model.news.SubjectTypeEntity;
import cn.com.czpb.client.module.news.adapter.NewsItemListViewAdapterNew;
import cn.com.czpb.client.module.news.view.TopImageView;
import cn.com.czpb.client.module.news.view.TopLanMuInZhuanTi;
import cn.com.czpb.client.net.Factory;
import cn.com.czpb.client.utils.LogUtils;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;


/**
 * Created by 猪猪侠 on 2017/8/25.
 */
public class ZhuantiActivity extends MToolBarActivity implements BaseQuickAdapter.OnItemChildClickListener, OnRefreshListener{
    @BindView(R.id.rv_id)
    RecyclerView rvId;
    @BindView(R.id.zhuanti_smartrefresh)
    SmartRefreshLayout smartrefresh;
    @BindView(R.id.zhuanti_name)
    TextView zhuantiname;
    private NewsItemListViewAdapterNew adapter;
    private String nid;
    private int page = 1;
    private int pageSize =15;
    private boolean mFlagRefresh;
    private int controlsx = 0;

    @Override
    protected int setLayoutId() {
        return R.layout.activity_zhuanti;
    }
    @Override
    public void initView() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        rvId.setLayoutManager(manager);
        adapter = new NewsItemListViewAdapterNew(activity,null,false);
        adapter.setOnItemChildClickListener(this);
        rvId.setAdapter(adapter);
        smartrefresh.setRefreshHeader(new ClassHeader_other(activity));
        smartrefresh.setOnRefreshListener(this);
        mImmersionBar.statusBarDarkFont(true).init();
        rvId.setFocusable(false);
//        smartrefresh.setFocusable(false);
    }



    @Override
    public void initData() {
        nid = getIntent().getStringExtra("nid");
        getColumns();
    }
    //专题栏目列表
    public void getColumns(){
        Map<String,String> map = new HashMap<>();
        map.put("sid",nid);
        Factory.provideHttpService().newsSubjectColumn(map)
                .subscribeOn(Schedulers.io())
                .filter(new Func1<SubjectTypeEntity, Boolean>() {
                    @Override
                    public Boolean call(SubjectTypeEntity entity) {
                        return "200".equals(entity.code);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<SubjectTypeEntity>() {
                    @Override
                    public void call(SubjectTypeEntity entity) {
                        smartrefresh.finishRefresh();
                        if ("200".equals(entity.code)){
                            adapter.removeAllHeaderView();
                            adapter.removeAllFooterView();
                            adapter.setNewData(null);
                            String imgurl = entity.data.getPhoto();
                            zhuantiname.setText(entity.data.getTitle());
                            adapter.addHeaderView(TopImageView.instance(ZhuantiActivity.this,imgurl).getView(),0);
                            getServerList(entity.data);
//                            for(int i=0;i<entity.data.getColumns().size();i++){
//                                getServerList(entity.data.getColumns().get(i),entity.data.getPhoto(),i);
//                            }
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        LogUtils.i("getColumns-->faild-->"+throwable);
                    }
                });


    }

    private void getServerList(final SubJectTypeBean data) {
        Map<String,String> map = new HashMap<>();
        map.put("cid", data.getColumns().get(controlsx).getCid());
        Factory.provideHttpService().newsSubjectNewsList(map)
                .subscribeOn(Schedulers.io())
                .filter(new Func1<NewsListEntity, Boolean>() {
                    @Override
                    public Boolean call(NewsListEntity entity) {
                        return "200".equals(entity.code);
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<NewsListEntity>() {
                    @Override
                    public void call(NewsListEntity entity) {
//                        setListData(entity.data);
                        TopLanMuInZhuanTi topLanMuInZhuanTi = TopLanMuInZhuanTi.instance(ZhuantiActivity.this);
                        topLanMuInZhuanTi.setData(entity.data);
                        topLanMuInZhuanTi.setLanmuname(data.getColumns().get(controlsx).getCname());
                        topLanMuInZhuanTi.setNewsurl(data.getColumns().get(controlsx).getNewsurl());
                        topLanMuInZhuanTi.setImgurl(data.getPhoto());
                        adapter.addHeaderView(topLanMuInZhuanTi.getView(),1+controlsx);
                        adapter.notifyDataSetChanged();
                        if(controlsx<entity.data.size()){
                            controlsx++;
                            getServerList(data);
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
    }

//    //TODO
//    private void getServerList(final SubjectItemColumnsBean sicb, final String imgurl, final int i){
//        Map<String,String> map = new HashMap<>();
//        map.put("cid", sicb.getCid());
//        Factory.provideHttpService().newsSubjectNewsList(map)
//                .subscribeOn(Schedulers.io())
//                .filter(new Func1<NewsListEntity, Boolean>() {
//                    @Override
//                    public Boolean call(NewsListEntity entity) {
//                        return "200".equals(entity.code);
//                    }
//                })
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<NewsListEntity>() {
//                    @Override
//                    public void call(NewsListEntity entity) {
////                        setListData(entity.data);
//                        TopLanMuInZhuanTi topLanMuInZhuanTi = TopLanMuInZhuanTi.instance(ZhuantiActivity.this);
//                        topLanMuInZhuanTi.setData(entity.data);
//                        topLanMuInZhuanTi.setLanmuname(sicb.getCname());
//                        topLanMuInZhuanTi.setNewsurl(sicb.getNewsurl());
//                        topLanMuInZhuanTi.setImgurl(imgurl);
//                        adapter.addHeaderView(topLanMuInZhuanTi.getView(),1+i);
//                        adapter.notifyDataSetChanged();
//                    }
//                }, new Action1<Throwable>() {
//                    @Override
//                    public void call(Throwable throwable) {
//
//                    }
//                });
//    }

    public void setListData(List<NewsBean> list) {
        if (mFlagRefresh) {
            adapter.setNewData(list);
            adapter.notifyDataSetChanged();
        } else {
            adapter.addData(list);
            adapter.notifyDataSetChanged();
            adapter.loadMoreComplete();
        }
        LogUtils.i("size-->" + adapter.getData().size());
        if (list.size() < pageSize) {
            //关闭刷新
            adapter.loadMoreEnd(true);
        }
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        final NewsBean newsBean = (NewsBean) adapter.getItem(position);
        switch (view.getId()) {
            case R.id.newsItem_root:
                ////REFACTOR: 2017/8/18 添加浏览量bug
                MyActivitymanager.pushActivity(activity, newsBean, newsBean.getTid(), "newsitem",newsBean.getRvalue());
                LogUtils.e("rtype--"+newsBean.getRtype());
                break;
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                mFlagRefresh = true;
                controlsx = 0;
                getColumns();
            }
        }, 2000);
    }
}
